package cn.wolfcode.shop.cloud.mq;


import cn.wolfcode.shop.cloud.ws.WebSocketServer;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@RocketMQMessageListener(
        consumerGroup = MQConstants.CREATE_ORDER_NOTIFY_GROUP,
        topic = MQConstants.SECKILL_ORDER_TOPIC,
        selectorExpression = MQConstants.CREATE_ORDER_SUCCESS_TAG + "||" + MQConstants.CREATE_ORDER_FAIL_TAG

)
@Component
public class OrderFailNotifyMQListener implements RocketMQListener<Map> {

    @Override
    public void onMessage(Map map) {
        try {
            System.out.println("进入到通知队列");
            Object uuid = map.get("uuid");
            //建立连接时可能非常快来不急建立，先睡一下
            Thread.sleep(1000);
            WebSocketServer webSocketServer = WebSocketServer.clients.get(uuid);
            webSocketServer.getSession().getBasicRemote().sendText(JSON.toJSONString(map));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
